﻿using UnityEngine;
using System.Collections;
using Qy_CSharp_NetWork.Component;
using Qy_CSharp_NetWork.Tools.Json;
using System;



public class NetWorkDataHandle : MonoBehaviour
{
    public event EventHandler BuildRoomSuccEvent;
    public event EventHandler NewMsgEvent_Login;
    public event EventHandler NewMsgEvent_Logout;
    public event EventHandler NewMsgEvent_Direction;
    public event EventHandler NewMsgEvent_Split;
    public event EventHandler NewMsgEvent_DeathBtn;
    public void JisightNetWorkHandle(object sender, LAMEventArgs evAgs)
    {
        Debug.Log("[SnakeGameInUnity]: ------- Receive Msg in --------");
        if (evAgs.message != null)
            Debug.Log(evAgs.message);
        switch (evAgs.status)
        {
            case DATA_STATUS_CODE.DATA_BUILD_ROOM_T:
                Debug.Log("[SnakeGameInUnity]: 建房 成功。");
                break;
            case DATA_STATUS_CODE.DATA_BUILD_ROOM_F:
                Debug.LogWarning("[SnakeGameInUnity]: 建房 失败。");
                break;
            case DATA_STATUS_CODE.DATA_ROOM_VERIFY_T:
                Debug.Log("[SnakeGameInUnity]: 验证 成功。:" + evAgs.message);
                if (BuildRoomSuccEvent != null)
                {
                    BuildRoomInfo buildRoomInfo = new BuildRoomInfo();
                    buildRoomInfo.qrCodeUrl = evAgs.message;
                    BuildRoomSuccEvent(this, buildRoomInfo);
                }
                break;
            case DATA_STATUS_CODE.DATA_ROOM_VERIFY_F:
                Debug.LogWarning("[SnakeGameInUnity]: 验证 失败。");
                break;
            case DATA_STATUS_CODE.DATA_ROOM_STATUS_T:
                Debug.Log("[SnakeGameInUnity]: 推送状态 成功。");
                break;
            case DATA_STATUS_CODE.DATA_ROOM_STATUS_F:
                Debug.LogWarning("[SnakeGameInUnity]: 推送状态 失败。");
                break;
            case DATA_STATUS_CODE.DATA_SUMMARIZE_T:
                Debug.Log("[SnakeGameInUnity]: 推送排行榜 成功。");
                break;
            case DATA_STATUS_CODE.DATA_SUMMARIZE_F:
                Debug.LogWarning("[SnakeGameInUnity]: 推送排行榜 失败。");
                break;
            case DATA_STATUS_CODE.DATA_AWARD_LIST_T:
                Debug.Log("[SnakeGameInUnity]: 请求奖励 成功。");
                break;
            case DATA_STATUS_CODE.DATA_AWARD_LIST_F:
                Debug.LogWarning("[SnakeGameInUnity]: 请求奖励 失败。");
                break;
            case DATA_STATUS_CODE.DATA_CUSTOM_MSG_T:
                Debug.Log("[SnakeGameInUnity]: 接收自定义数据(交互) 成功。");
                GameDataFormat(evAgs.message);
                break;
            case DATA_STATUS_CODE.DATA_CUSTOM_MSG_F:
                Debug.LogWarning("[SnakeGameInUnity]: 接收自定义数据 失败。");
                break;
            case DATA_STATUS_CODE.ERROR_TRIGGER_CODE:
                Debug.LogWarning("[SnakeGameInUnity]: 链接出现错误->错误代码 ：" + evAgs.message);
                break;
            default:
                Debug.LogWarning("[SnakeGameInUnity]: dll出现了 未知 类型！！ ");
                break;
        }
        Debug.Log("[SnakeGameInUnity]: ------- Receive Msg out --------");
    }
    public void GameDataFormat(string msg)
    {
        Debug.Log("GameDataFormat():" + msg);
        JsonDataQy _jdata = null;
        try
        {
            _jdata = JsonToolsQy.GetJsonData(msg);
        }
        catch (Exception ee)
        {
            Debug.LogWarning("[JsonToolsQy]:" + ee.Message);
            Debug.LogWarning("[JsonToolsQy]:" + "it's not a json !!!!");
            return;
        }

        if (_jdata["type"] == null)
        {
            Debug.LogWarning("Json information' type is 'null': please check data from H5.");
            return;
        }
        if (_jdata["userId"] == null)
        {
            Debug.LogWarning("Json information' userId is 'null': please check data from H5.");
            return;
        }
        switch ((RECV_MESSAGE_TYPE)(int.Parse(_jdata["type"].ToString())))
        {
            case RECV_MESSAGE_TYPE.LOGIN: // 登陆信息处理
                Debug.Log("登陆 信息");
                LoginInfo loginInfo = new LoginInfo();
                if (_jdata["portraitUrl"] == null || _jdata["nickName"] == null)
                {
                    Debug.LogWarning(@"'Login information 'wrong:Please check data from H5 ,
                                           verify 'headPortraitUrl'\'nickName'");
                    return;
                }
                loginInfo.userId = _jdata["userId"].ToString();
                loginInfo.headPortraitUrl = _jdata["portraitUrl"].ToString();
                loginInfo.nickName = _jdata["nickName"].ToString();
                if (NewMsgEvent_Login != null)
                {
                    NewMsgEvent_Login(this, loginInfo);
                }
                break;
            case RECV_MESSAGE_TYPE.LOGOUT: // 退出信息处理
                Debug.Log(" 退出 信息");
                LogoutInfo logoutInfo = new LogoutInfo();
                logoutInfo.userId = _jdata["userId"].ToString();
                if (NewMsgEvent_Logout != null)
                {
                    NewMsgEvent_Logout(this, logoutInfo);
                }
                break;
            case RECV_MESSAGE_TYPE.DIRECTION:  // 方向信息处理
                Debug.Log("方向 信息");
                if (_jdata["data"] == null)
                {
                    Debug.LogWarning("direct information 'wrong:Please check data from H5 ,  verify 'data'");
                    return;
                }
                if (_jdata["data"]["x"] == null || _jdata["data"]["y"] == null)
                {
                    Debug.LogWarning(@"'direct information 'wrong:Please check data from H5 ,  verify 'x'\'y'");
                    return;
                }
                DirectionInfo directInfo = new DirectionInfo();
                directInfo.userId = _jdata["userId"].ToString();
                directInfo.dir_x = float.Parse(_jdata["data"]["x"].ToString());
                directInfo.dir_y = -float.Parse(_jdata["data"]["y"].ToString());
                if (NewMsgEvent_Direction != null)
                {
                    NewMsgEvent_Direction(this, directInfo);
                }
                break;
            case RECV_MESSAGE_TYPE.SPLITE: // 加速信息处理
                Debug.Log(" 加速 信息");
                if (_jdata["data"] == null)
                {
                    Debug.LogWarning(@"'Accelerate information 'wrong:Please check data from H5 ,
                                           verify 'isAccelerate'");
                    return;
                }
                SpliteInfo accelerateInfo = new SpliteInfo();
                accelerateInfo.userId = _jdata["userId"].ToString();
                if (_jdata["data"].ToString() == "1")
                {
                    accelerateInfo.isAccelerate = true;
                }
                else
                {
                    accelerateInfo.isAccelerate = false;
                }
                if (NewMsgEvent_Split != null)
                {
                    NewMsgEvent_Split(this, accelerateInfo);
                }
                break;
            case RECV_MESSAGE_TYPE.DEATH_BTN:
                Debug.Log("死亡后的选择信息");
                if (_jdata["data"] == null)
                {
                    Debug.LogWarning(@"'Death information 'wrong:Please check data from H5 ,
                                           verify 'isAccelerate'");
                    return;
                }
                DeathBtnInfo deathInfo = new DeathBtnInfo();
                deathInfo.userId = _jdata["userId"].ToString();
                if (_jdata["data"].ToString() == "0")
                {
                    deathInfo.isOver = true;        //结束游戏
                }
                else
                {
                    deathInfo.isOver = false;        //再来一次
                }
                if (NewMsgEvent_DeathBtn != null)
                {
                    NewMsgEvent_DeathBtn(this, deathInfo);
                }

                break;
            default:
                Debug.LogWarning("'type' value is wrong , please check data from H5.");
                break;
        }

    }

}
